44. Solution: More Weather Details

More Weather Details Solution

In this exercise, you've uses CursorLoaders to display more weather information in the Detail Layout.

Here's the solution code for the onCreateLoader:

@Override
 public Loader<Cursor> onCreateLoader(int loaderId, Bundle loaderArgs) {
     switch (loaderId) {
         case ID_DETAIL_LOADER:
             return new CursorLoader(this,
                        mUri,
                        WEATHER_DETAIL_PROJECTION,
                        null,
                        null,
                        null);
         default:
             throw new RuntimeException("Loader Not Implemented: " + loaderId);
     }
 }

onLoadFinished should start by checking if cursor has valid data:

boolean cursorHasValidData = false;
if (data != null && data.moveToFirst()) {
   /* We have valid data, continue on to bind the data to the UI */
   cursorHasValidData = true;
}
if (!cursorHasValidData) {
   /* No data to display, simply return and do nothing */
   return;
}

Then for each piece of weather information, retrieve it from the cursor and display it in the appropriate view.
For example, the fist text view should the display the date as follows:

long localDateMidnightGmt = data.getLong(INDEX_WEATHER_DATE);
String dateText = SunshineDateUtils.getFriendlyDateString(this, localDateMidnightGmt, true);
mDateView.setText(dateText);

To view all the changes to the ForecastAdapter, MainActivity and the new layout code, see the solution and comparison code linked below.

Solution Code

Solution: [S09.05-Solution-MoreDetails][Diff]